﻿using System;
using System.Globalization;
using System.Web.UI.WebControls;
using ContractManagementSystem.BusinessLogicLayer;
using ContractManagementSystem.EntityObjects;

namespace ContractManagementSystem.Presentation.ReportManagement
{
    public partial class ReportContract : System.Web.UI.Page
    {
        private int _rollId;
        private string _userName;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserPermission();

            if (Session["RoleId"] != null)
            {
                if (!IsPostBack)
                {
                    BindListContract();
                    BindContractNumber();
                }
            }
        }

        protected void BindListContract()
        {
            _rollId = int.Parse(Session["RoleId"].ToString(), CultureInfo.InvariantCulture);
            _userName = Session["UserName"].ToString();

            if (_rollId == 1)
            {
                var result = ContractLogic.ListActiveContracts();
                ListViewContract.DataSource = result;
                ListViewContract.DataBind();
                Session["SOURCE_CONTRACT_TABLE"] = result;
            }
            else if(_rollId==4)
            {
                var result = ContractLogic.ListActiveContractsByConsultant(_userName);
                ListViewContract.DataSource = result;
                ListViewContract.DataBind();
                Session["SOURCE_CONTRACT_TABLE"] = result;
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            ButtonSearchClick(null, null);
            ListViewContract.DataBind();
        }

        protected void BindContractNumber()
        {
            _rollId = int.Parse(Session["RoleId"].ToString(), CultureInfo.InvariantCulture);
            _userName = Session["UserName"].ToString();

            if (_rollId == 1)
            {
                var result = ContractLogic.ListActiveContracts();
                dropContractTitle.DataSource = result;
            }
            else if(_rollId==4)
            {
                var result = ContractLogic.ListActiveContractsByConsultant(_userName);
                dropContractTitle.DataSource = result;
            }

            dropContractTitle.DataTextField = "ContractNumber";
            dropContractTitle.DataValueField = "ContractNumber";
            dropContractTitle.DataBind();
            dropContractTitle.Items.Insert(0,new ListItem("All","All"));
        }


        // Check user's permissions
        private void CheckUserPermission()
        {
            var userName = CommonLogic.ConvertToString(Session["UserName"]);
            const int functionId = (int) FunctionList.AllReport;

            var urlToCome = Page.Master.ResolveUrl("Presentation/AuthorizationError.aspx");

            AuthorizationChecker.IsValidAuthorization(userName, functionId, Response, urlToCome);
        }

        /// <summary>
        /// Filter search contract
        /// </summary>
        protected void FilterContract()
        {
            _rollId = int.Parse(Session["RoleId"].ToString(), CultureInfo.InvariantCulture);
            _userName = Session["UserName"].ToString();

            var contractNumber = dropContractTitle.SelectedValue;

            var contract = new ContractEntity
                               {
                                   ContractNumber = contractNumber,
                                   Status = 1
                               };
            if (_rollId == 1)
            {
                var query = ContractLogic.ReportSearchContract(contract);

                if (query == null)
                {
                    return;
                }

                Session["SOURCE_CONTRACT_TABLE"] = query;

                ListViewContract.DataSource = query;
            }
            else if(_rollId==4)
            {
                var query = ContractLogic.ReportSearchContractByConsultant(_userName,contract);

                if (query == null)
                {
                    return;
                }

                Session["SOURCE_CONTRACT_TABLE"] = query;

                ListViewContract.DataSource = query;
            }
            ListViewContract.DataBind();
        }

        protected void ButtonCreateReportClick(object sender, EventArgs e)
        {
            Response.Redirect("ShowReportContract.aspx");
        }

        protected void ButtonSearchClick(object sender, EventArgs e)
        {
            FilterContract();
        }

    }
}